package com.inspector.app.ui.my

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import com.example.commen.base.BaseFragment
import com.example.commen.utile.SingleClick
import com.hjq.toast.Toaster
import com.inspector.app.R
import com.inspector.app.databinding.FragmentMyBinding
import com.inspector.app.di.upversion.VersionModel
import com.inspector.app.ui.widget.BindingAdapter
import com.inspector.app.ui.widget.ExitSettingDialog
import com.inspector.app.util.CommenArgs

/**
 * @Author houff
 * @Date 2024/3/29 11:53
 */
class MyFragment : BaseFragment() {

    private lateinit var _binding: FragmentMyBinding
    private var exitSettingDialog: ExitSettingDialog? = null
    private val versionViewModel: VersionModel by viewModels()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentMyBinding.inflate(inflater, container, false)
        initView()
        initLisenter()
        return _binding.root
    }

    @SuppressLint("SetTextI18n", "CheckResult")
    override fun initData() {
        if (CommenArgs.userInfo != null) {
            val icon =
                if (CommenArgs.userInfo?.sex == 1) R.mipmap.iv_avatar else R.mipmap.iv_avatar_lady
            val headUrl =
                if (CommenArgs.userInfo?.headUrl?.isNotBlank() == true) CommenArgs.userInfo?.headUrl else icon
            BindingAdapter.loadImage(_binding.imgMyHomeIcon, headUrl, icon)
            _binding.tvMyHomeName.text = "Hi~  " + CommenArgs.userInfo?.realname
            _binding.tvMyHomeTips.text = CommenArgs.userInfo?.phoneNum
        }

        versionViewModel.VersionState.observe(this) {
            if (it?.isUpdate == false) {
                Toaster.show("已经是最新版本了")
            }
        }
    }

    private fun initView() {
        exitSettingDialog = ExitSettingDialog(requireContext())
    }

    private fun initLisenter() {
        SingleClick(_binding.rlLogoutyLogin) {
            //退出登录操作 暂时以前方法写 未与viewmodel绑定处理
//            viewModel.loginOut()
            exitSettingDialog?.show()
        }
        /**版本更新*/
        SingleClick(_binding.llUpdate) {
            versionViewModel.isUpgrade(requireActivity() as AppCompatActivity)
        }
        SingleClick(_binding.rlAbout) {
            this.findNavController().navigate(R.id.action_to_about)
        }
    }
}